package com.xiangb.springbootjpa.controller;

import com.xiangb.springbootjpa.common.PageVO;
import com.xiangb.springbootjpa.dao.PersonRepository;
import com.xiangb.springbootjpa.entity.Person;
import com.xiangb.springbootjpa.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author xb
 * @descripton:
 * @date 2020/12/20
 */
@RestController
@RequestMapping("/person")
public class PersonController {
    @Autowired
    private PersonRepository personRepository;
    @Autowired
    private PersonService personService;

    @GetMapping(value = "")
    public List<Person> getPersonList() {
        return personRepository.findAll();
    }

    /**
     * 添加一个人员
     *
     * @param name
     * @param age
     * @return
     */
    @PostMapping(value = "")
    public Person personAdd(@RequestParam("name") String name,
                            @RequestParam("age") Integer age) {
        Person person = new Person();
        person.setName(name);
        person.setAge(age);

        return personRepository.save(person);
    }

    /**
     * 查询一个人员
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public Person personFindOne(@PathVariable("id") Integer id) {
        return personService.findById(id);
    }

    /**
     * 删除一个人员
     *
     * @param id
     */
    @DeleteMapping(value = "/{id}")
    public void personDelete(@PathVariable("id") Integer id) {
        personRepository.deleteById(id);
    }

    /**
     * 更新一个人员
     *
     * @param id
     * @param name
     * @param age
     * @return
     */
    @PutMapping(value = "/{id}")
    public Person personUpdate(@PathVariable("id") Integer id,
                               @RequestParam("name") String name,
                               @RequestParam("age") Integer age) {
        Person person = new Person();
        person.setId(id);
        person.setName(name);
        person.setAge(age);
        return personRepository.save(person);
    }

    /**
     * 通过年龄来查询
     * @param age
     * @return
     */
    @GetMapping(value = "/age/{age}")
    public List<Person> personListByAge(@PathVariable("age") Integer age) {
        return personRepository.findByAge(age);
    }

    /**
     * 通过年龄来查询
     * @param age
     * @return
     */
    @GetMapping(value = "/name/{name}")
    public Person personListByAge(@PathVariable("name") String name) {
        return personRepository.findByName(name);
    }

    /**
     * 事务测试
     */
    @PostMapping("/two")
    public void personTwo(){
        personService.insertTwo();
    }

    /**
     * 分页查询列表
     * @param name
     * @param age
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/page")
    public PageVO<Person> personUpdate(@RequestParam("name") String name,
                                      @RequestParam("age") Integer age,
                                      @RequestParam("pageNumber") Integer pageNumber,
                                      @RequestParam("pageSize") Integer pageSize) {
        return personService.findByPage(pageNumber, pageSize, name, age);
    }
}
